********************************************************************************
**	PURPOSE: Predict CBL take-up, create AT2
**							
**	INPUTS: Wide with outcomes
**	
**	OUTPUTS: Predict CBL Takeup Tests
**			 
**  CREATED/EDITED BY: Kayla Wilding, Leah Kim
**
**	NOTE: If the output file saves to dropbox, box, etc., and needs to sync after each putexcel then you need to leave the "sleep" commands in the file. If you run this on a local file then you can remove the sleeps to run it faster.
**
**	DATE CREATED: 7/2018
**
**	DATE LAST EDITED: 1/30/2018
********************************************************************************

/*******************************************************************************
0. Setup 
********************************************************************************/
clear all 

*By characteristic need to do a diff and diff between the takeup and nontakup 
local takeupvars age1 female1 married1 adults1 child1 race_black1 college1 z_risk_i1 ///
 z_contknow_i1 z_liqcf_i1 ficoscore081 z_credac_i1 z_credac_rev_i1 prevloans1

putexcel set "$outputtables/Predict CBL Takeup Tests.xlsx", replace
use "$adta/Wide with outcomes.dta", clear
local rows : word count `takeupvars'
local rows = `rows' * 2

label var z_risk_i1 "Risk scale"
label var z_contknow_i1 "Self control and knowledge index"
label var z_liqcf_i1 "Liquid index"
label var z_credac_i1 "Installment Credit Access at baseline index"
label var z_credac_rev_i1 "Revolving Access at baseline index"
label var prevloans1 "Previous Loans"
matrix T = J(`rows',9,.)

*Before: No Takeup 
*After: Takeup 
*			Before (period == 0)	After (period == 1)
*Control 	Extra Step, No takeup	CBL Group, No takeup
*Treatment 	Extra Step, Open CBL 	CBL Group, Open cbl 
local count 1
foreach var in `takeupvars' {
	local countse = `count' + 1 
	diff `var' if flag_randomized == 1 ,  t(op18) p(enc)
		matrix T[`count',1] = r(mean_t1)
		matrix T[`countse',1] = r(se_t1)
		matrix T[`count',2] = r(diff1) 
		matrix T[`countse',2] = r(se_d1)	

		matrix T[`count',5] =  r(mean_t0)
		matrix T[`countse',5] =  r(se_t0)
		matrix T[`count',6] = r(diff0)
		matrix T[`countse',6] = r(se_d0)

	**diff doesnt store the p-value so need to run ttest but it would be equivalent to using the p-value in DiD 
	*Test between take up and no take up for control and cbl group 
	ttest `var' if flag_cblgroup == 1 , by(op18)
		matrix T[`count',3] = r(p)

	ttest `var' if flag_extragroup == 1, by(op18)
		matrix T[`count',7] = r(p)

	*Test the percent that took up bewteen treatment and control 
	ttest `var' if flag_randomized == 1 & op18 == 1, by(enc)	
		matrix T[`count', 9] = r(p) 

	local ++count 
	local ++count
}

mat list T

putexcel A5 = "Age", font(Calibri, 12) 
	sleep 1000
putexcel A7 = "Female", font(Calibri, 12) 
	sleep 1000
putexcel A9 = "Married", font(Calibri, 12) 
	sleep 2000
putexcel A11 = "# Adults in HH", font(Calibri, 12) 
	sleep 1500
putexcel A13 = "# Children in HH", font(Calibri, 12) 
	sleep 1500
putexcel A15 = "Race - Black", font(Calibri, 12) 
	sleep 2000
putexcel A17 = "College or more", font(Calibri, 12) 
	sleep 1500
putexcel A19 = "Financial Risk-Taking scale (standardized)", font(Calibri, 12) 
	sleep 1500
putexcel A21 = "Self-Control and Credit Knowledge Index (standardized)", font(Calibri, 12) 
	sleep 1500
putexcel A23 = "Liquid Index (standardized)", font(Calibri, 12) 
	sleep 1500
putexcel A25 = "Baseline FICO Score", font(Calibri, 12)
	sleep 1500
putexcel A27 = "Installment Credit Access at baseline index", font(Calibri, 12)
	sleep 1500
putexcel A29 = "Revolving Access at baseline index", font(Calibri, 12)
	sleep 1500
putexcel A31 = "Previous Loans", font(Calibri, 12)
	sleep 1500
	
putexcel B1 = "(1)", font(Calibri, 12) hcenter
	sleep 1500
putexcel C1 = "(2)", font(Calibri, 12) hcenter
	sleep 1500
putexcel D1 = "(3)", font(Calibri, 12) hcenter
	sleep 1500
putexcel F1 = "(4)", font(Calibri, 12) hcenter
	sleep 1500
putexcel G1 = "(5)", font(Calibri, 12) hcenter
	sleep 3000
putexcel H1 = "(6)", font(Calibri, 12) hcenter
	sleep 1500
putexcel J1 = "(7)", font(Calibri, 12) hcenter
	sleep 1500
putexcel B2:G2 = "CBL Group", merge hcenter font(Calibri, 12) border(bottom)
	sleep 1500
putexcel I2:N2 = "Extra-Step Group", merge hcenter font(Calibri, 12) border(bottom)
	sleep 1500
putexcel B3:D3 = "Full Sample", merge hcenter 
	sleep 1500
putexcel I3:K3 = "Full Sample", merge hcenter 
	sleep 1500
putexcel B4 ="Takeup" 
	sleep 1500
putexcel F4 = "Takeup" 
	sleep 1500
putexcel C4 = "Takeup-No Takeup" 
	sleep 1500
putexcel F4 = "Takeup-No Takeup" 
	sleep 1500
putexcel D4 = "P-value of 1 = 2"
	sleep 1500
putexcel H4 = "P-value of 4 = 5"
	sleep 1500
putexcel J3 = "P-value" 
	sleep 1500
putexcel J4 = "of 1 = 7"
	sleep 1500
putexcel B5 = matrix(T),  font(Calibri, 12) hcenter nformat("0.000")

**EOF**
